@echo off

cls

rem -----------------------------------------------------------------------------

set MODE=%~1
set ADDRESSMODEL=%~2
set ACTION=%~3
set SOURCE_DIRECTORY=%~4
set GENERATOR=%~5
set TOOLSET=%~6
set COMPILER_TAG=%~7

rem -----------------------------------------------------------------------------

if "%NAME_PRODUCT%"     == ""   for %%a in (.) do set NAME_PRODUCT=%%~na
if "%SOURCE_DIRECTORY%" == ""   set SOURCE_DIRECTORY=%~dp0
if "%GENERATOR%"        == ""   set GENERATOR=Visual Studio 13
if "%TOOLSET%"          == ""   set TOOLSET=CTP_Nov2013
if "%MODE%"             == ""   set MODE=Debug
if "%ADDRESSMODEL%"     == ""   set ADDRESSMODEL=32
if "%ACTION%"           == ""   set ACTION=launch
if "%ADDRESSMODEL%"     == "64" set SUFFIX= Win64
if "%ADDRESSMODEL%"     == "32" set SUFFIX=
if "%BUILDDIR%"         == ""   set BUILDDIR=%MODE%%ADDRESSMODEL%
if "%COMPILER_TAG%"     == ""   set COMPILER_TAG=msvc

if "%GENERATOR%" == "Visual Studio 11" set COMPILER_TAG=msvc2012
if "%GENERATOR%" == "Visual Studio 12" set COMPILER_TAG=msvc2013

echo PROJECT NAME ......... %NAME_PRODUCT%
echo SOURCE DIRECTORY ..... %SOURCE_DIRECTORY%
echo LOCATION ............. %COMPILER_TAG%-%MODE%-%ADDRESSMODEL%-%NAME_PRODUCT%
echo IDE .................. %GENERATOR%%SUFFIX%
echo TOOLSET .............. %TOOLSET%
echo BUILD TYPE ........... %MODE%
echo ADDRESS MODEL ........ %ADDRESSMODEL%
echo ACTION ............... %ACTION%

rem -----------------------------------------------------------------------------

if not exist build ( mkdir build )
cd build

set _build_dir=%COMPILER_TAG%-%MODE%-%ADDRESSMODEL%-%NAME_PRODUCT%
if not exist %_build_dir% ( mkdir %_build_dir% )
cd %_build_dir%

rem -----------------------------------------------------------------------------

cmake  -G "%GENERATOR%%SUFFIX%" ^
       -T "%TOOLSET%" ^
       -DCMAKE_BUILD_TYPE=%MODE% ^
        %SOURCE_DIRECTORY%

rem -----------------------------------------------------------------------------

echo [ %ACTION% ] ...

if "%ACTION%" == "stop" exit/b

if "%GENERATOR%" == "Visual Studio 11" (
    set vsvars1=C:\"Program Files (x86)\Microsoft Visual Studio 11.0"\Common7\Tools\vsvars32.bat
    set vsvars2=C:\"Program Files\Microsoft Visual Studio 11.0"\Common7\Tools\vsvars32.bat
    if exist %vsvars1% ( call %vsvars1% )
    if exist %vsvars2% ( call %vsvars2% )
)
if "%GENERATOR%" == "Visual Studio 12" (
    set vsvars1=C:\"Program Files (x86)\Microsoft Visual Studio 12.0"\Common7\Tools\vsvars32.bat
    set vsvars2=C:\"Program Files\Microsoft Visual Studio 12.0"\Common7\Tools\vsvars32.bat
    if exist %vsvars1% ( call %vsvars1% )
    if exist %vsvars2% ( call %vsvars2% )
)

if "%ACTION%" == "launch" ( 
    devenv %NAME_PRODUCT%.sln 
) ELSE ( 
    devenv %NAME_PRODUCT%.sln /%ACTION% %MODE%
    if exist INSTALL.vcxproj (
        devenv %NAME_PRODUCT%.sln /%ACTION% %MODE% /project INSTALL.vcxproj /projectconfig %MODE%
    )
)

rem -----------------------------------------------------------------------------

cd ..
cd ..

exit/b
       

